package org.xbzheng.restful_study.web;

import org.apache.commons.collections.map.HashedMap;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.xbzheng.restful_study.model.Resources;
import org.xbzheng.restful_study.model.Role;
import org.xbzheng.restful_study.model.User;
import org.xbzheng.restful_study.service.ResourceService;
import org.xbzheng.restful_study.util.ScrumConstant;

import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2016/12/6.
 */
@Controller
@RequestMapping("/template")
public class TemplateController {

    @Autowired
    private VelocityEngine velocityEngine;

    @Autowired
    private ResourceService resourceService;

    @RequestMapping("/resource/{owner}")
    public String getContent(@RequestParam String owner, HttpSession session){
        User user = (User) session.getAttribute(ScrumConstant.USER);
        Set<Role> roles = user.getRoles();

        final Set<Resources> resources = new HashSet<>();
        roles.stream().forEach(role -> {
            resources.addAll(role.getResources());
        });

        List<Resources> authorities = resourceService.findPageResources(user.getUsername(), owner);



        Map<String, Object> model = new HashedMap();
        model.put("resources", resources);
        model.put("authorities", authorities);

        String template = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "", "utf-8", model);

        return null;
    }
}
